# -*- coding: utf-8 -*-
from ConfigUtil import ConfigIni
import FileUtils as fileUtils
import yaml
import traceback
import os

# import sys
# reload(sys)
# sys.setdefaultencoding('utf8')

def case_yaml_file(path=""):
    '''
    获取指定文件夹下所有的yaml文件
    :return:
    '''
    if path!="":
        yaml_path=path
    else:
        ini=ConfigIni()
        yaml_path=ini.get_ini('test_case','case')
    return fileUtils.all_file_path(yaml_path,'.yaml')


global_setting=''
def get_yaml_info(path_yaml):
    '''
    :param path_yaml: 测试用例地址
    :return: 返回yaml内字典，
    '''
    inherit_case_file = case_yaml_file()
    # print(inherit_case_file)

    def get_case(path_yaml,read_header=True):
        case_list = []
        try:
            with open(unicode(path_yaml,"utf-8").encode("gbk")) as f:
                data = yaml.load(f)
                # print(data)
                if read_header:
                    if 'settings' in data:
                        global global_setting
                        global_setting=data.get('settings')
                data = data.get('script_steps')
                for item in data:
                    if isinstance(item, dict):
                        # print(item)
                        if 'test_inherit' in item:
                            inherit_case_name = item['test_inherit']
                            inherit_case = inherit_case_name
                            if inherit_case in inherit_case_file.keys():
                                case_list += case_list + get_case(inherit_case_file[inherit_case],read_header=False)[1]
                        else:
                            case_list.append(item)
                    else:
                        pass
        except Exception as e:
            traceback.print_exc()
        return global_setting, case_list

    return get_case(path_yaml)

from ErrorUtil import error

@error()
def read_yaml(file_path):
    if not os.path.exists(file_path):
        print("文件不存在")
        return None
    with open(file_path) as f:
        data=yaml.load(f)
        return data

if __name__ == '__main__':
    # cpu_mem,items=get_yaml_info('E:/python/project/FFAutoTest/TestCase/001.yaml')
    # print(cpu_mem)
    # print(items)
    # result=read_yaml(unicode('E:\\python\project\\TerminateAutoTest\\TestCase\\分类.yaml',"utf-8").encode("gbk"))
    result = read_yaml('E:\\python\project\\TerminateAutoTest\\TestCase\\001weather.yaml')
    print(result)
    # print(result["script_steps"][0]["test_desc"])
    # print(type(result['script_steps'][1]['locations'][1]))

    # print(result['script_steps'][1])
    #
    # print(type(eval(result['script_steps'][1]['data']["rgbs"][0])))

    # print(result["script_steps"][0]["num"].startswith("$"))
    # tmp=result["script_steps"][0]["num"]
    # s=tmp.replace("${", "")
    # print(s)
    #
    # import re
    # s1=re.search("\${(.*)}",tmp)
    # print(s1.group(1))

    # s1=result['script_steps']
    # # print(s1[7]['area'])
    # t=s1[7]['area']
    # t1=t.split(",")
    # print(t1)
    # try:
    #     with open('E:/python/project/FFAutoTest/TestCase/movie/84.yaml', encoding='UTF-8') as f:
    #         data = yaml.load(f)
    #         print(data)
    # except Exception as e:
    #     traceback.print_exc()
    # pass